<template>
  <el-dialog
    v-dialogDrag
    ref="dialog__wrapper"
    :title="mode | modeFilter"
    :width="width"
    :visible.sync="dialogFormVisible"
    :close-on-click-modal="false"
    custom-class="r-dialog"
    top="5vh"
    append-to-body
  >
    <slot :mode="mode"/>
    <div v-if="showFooter" slot="footer" class="dialog-footer" align="center">
      <el-button type="text">
        <i class="el-icon-search magnifier"/>
      </el-button>

      <el-button v-show="confirmShow" :loading="btnLoad" type="primary" @click="confirm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import _ from 'lodash'

export default {
  name: 'BaseDialog',
  filters: {
    modeFilter(val) {
      const title = {
        ADD: '新增',
        EDIT: '编辑',
        DETAIL: '明细',
        AUTH: '分配权限',
        REFUND: '退款',
        INFO: '详情',
        RECHARGE: '充值',
        WITHDRAW: '提现'
      }
      return title[val] ? title[val] : val
    }
  },
  props: {
    confirmShow: {
      type: Boolean,
      default: true
    },
    width: {
      type: String,
      default: ''
    },
    // appendToBody:{
    //   type:Boolean,
    //   default:false
    // },
    showFooter: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      dialogFormVisible: false,
      btnLoad: false,
      mode: null,
      originalData: null,
      form: null
    }
  },
  methods: {
    confirm(e) {
      debugger
      if (this.form && this.originalData) {
        const isEqual = _.isEqual(this.form, this.originalData)
        if (isEqual) {
          this.close()
          return
        }
      }
      debugger
      this.$emit('confirm-handle', this, e)
    },
    cancel(e) {
      this.$emit('cancel-handle', this, e)
      this.close()
    },
    wait() {
      this.btnLoad = true
      return this
    },
    complete() {
      this.btnLoad = false
      return this
    },
    open(mode, data) {
      this.mode = mode
      this.dialogFormVisible = true
      if (data) {
        this.form = data
        this.originalData = _.cloneDeep(data)
      } else {
        this.form = null
        this.originalData = null
      }
    },
    close() {
      this.mode = null
      this.dialogFormVisible = false
    }
  }
}
</script>

<style scoped>
</style>
